package io.github.scarletsky.bangumi.ui.activities; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.view.KeyEvent; import android.view.View; import android.view.Window; import android.webkit.WebChromeClient; import android.webkit.WebSettings; import android.webkit.WebView; import android.webkit.WebViewClient; import com.rey.material.widget.ProgressView; import io.github.scarletsky.bangumi.R; import io.github.scarletsky.bangumi.api.BangumiApi; import io.github.scarletsky.bangumi.utils.ToastManager; /** * Created by scarlex on 15-7-15. */ public class WebviewActivity extends AppCompatActivity { private WebView mWebview; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_webview); mWebview = (WebView) findViewById(R.id.webview); final ProgressView mProgress = (ProgressView) findViewById(R.id.progress); WebSettings settings = mWebview.getSettings(); settings.setJavaScriptEnabled(true); settings.setSupportZoom(false); settings.setBuiltInZoomControls(false); mWebview.setWebViewClient(new WebViewClient() { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); return true; } @Override public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { ToastManager.show(WebviewActivity.this, description); } @Override public void onPageFinished(WebView view, String url) { super.onPageFinished(view, url); } }); mWebview.setWebChromeClient(new WebChromeClient() { @Override public void onProgressChanged(WebView view, int newProgress) { if (newProgress == 100) { mProgress.setVisibility(View.GONE); } else { if (mProgress.getVisibility() == View.GONE) { mProgress.setVisibility(View.VISIBLE); } mProgress.setProgress((float) (newProgress / 100.0)); } } }); String subjectUrl = getIntent().getStringExtra("subjectUrl"); if (subjectUrl != null) { mWebview.loadUrl(subjectUrl); } else { mWebview.loadUrl(BangumiApi.URL_RAKUEN); } } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebview.canGoBack()) { mWebview.goBack(); return true; } return super.onKeyDown(keyCode, event); } }